Spring Boot Client তৈরি করার সময় কিছু best practices অনুসরণ করা ভালো, যা কোডের মান, কর্মক্ষমতা এবং নিরাপত্তা বৃদ্ধি করতে সহায়তা করে। নিচে Spring Boot Client ডেভেলপ করার সময় কিছু সাধারণ best practices এবং উদাহরণ দেওয়া হয়েছে।
WebClient হল Spring WebFlux এর অংশ, যা নন-ব্লকিং এবং রিয়্যাক্টিভ অপারেশন সমর্থন করে, যেখানে RestTemplate ব্লকিং I/O অপারেশন হ্যান্ডল করে। আপনি যদি অ্যাসিনক্রোনাস এবং রিয়্যাক্টিভ অ্যাপ্লিকেশন তৈরি করেন, তাহলে WebClient ব্যবহার করা উচিৎ।
Best Practice:
@Autowired
বা @Bean
দিয়ে ইনস্ট্যান্স তৈরি করুন, এবং যথাযথ Exception Handling প্রয়োগ করুন।API কল গুলি Service Layer এ রাখা উচিত, এবং Controller Layer শুধু HTTP রিকোয়েস্ট হ্যান্ডল করবে। এর ফলে কোড পুনরায় ব্যবহারযোগ্য হয় এবং টেস্টিং সহজ হয়।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class ApiService {
private final WebClient webClient;
@Autowired
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
public String fetchPosts() {
return webClient.get()
.uri("/posts")
.retrieve()
.bodyToMono(String.class)
.block(); // block() can be avoided in a real async use case
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ApiController {
private final ApiService apiService;
@Autowired
public ApiController(ApiService apiService) {
this.apiService = apiService;
}
@GetMapping("/posts")
public String getPosts() {
return apiService.fetchPosts();
}
}
API কলের রেসপন্স সঠিকভাবে হ্যান্ডল করা জরুরি, বিশেষ করে যখন ত্রুটি বা অপ্রত্যাশিত ডেটা আসে। ResponseEntity
ব্যবহার করে কাস্টম HTTP স্ট্যাটাস কোড সহ রেসপন্স প্রদান করা উচিত।
Best Practice:
try-catch
ব্লক ব্যবহার করুন এবং ResponseEntity
দিয়ে কাস্টম HTTP স্ট্যাটাস কোড দিন।import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
public ResponseEntity<String> fetchPosts() {
try {
String response = webClient.get()
.uri("/posts")
.retrieve()
.bodyToMono(String.class)
.block();
return ResponseEntity.ok(response);
} catch (WebClientResponseException e) {
return ResponseEntity.status(e.getStatusCode()).body("Error: " + e.getMessage());
}
}
}
যখন আপনি দীর্ঘ সময় ধরে চলা বা I/O-বাউন্ড অপারেশন (যেমন API কল) করছেন, তখন অ্যাসিনক্রোনাস অপারেশন ব্যবহার করা উচিত যাতে এটি থ্রেড ব্লক না করে। WebClient এবং Mono/Flux ব্যবহার করে আপনি অ্যাসিনক্রোনাস অপারেশন হ্যান্ডল করতে পারেন।
Best Practice:
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
}
public Mono<String> fetchPostsAsync() {
return webClient.get()
.uri("/posts")
.retrieve()
.bodyToMono(String.class);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class ApiController {
private final ApiService apiService;
@Autowired
public ApiController(ApiService apiService) {
this.apiService = apiService;
}
@GetMapping("/posts")
public Mono<String> getPosts() {
return apiService.fetchPostsAsync();
}
}
API কলের জন্য টাইমআউট এবং রিট্রাই লজিক ব্যবহার করা গুরুত্বপূর্ণ, বিশেষ করে যখন আপনার অ্যাপ্লিকেশন বাইরের সার্ভিসের উপর নির্ভরশীল থাকে। আপনি WebClient-এ timeout
এবং retry
লজিক যোগ করতে পারেন।
Best Practice:
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;
import java.time.Duration;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com")
.build();
}
public Mono<String> fetchPostsWithRetry() {
return webClient.get()
.uri("/posts")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5)) // Timeout after 5 seconds
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(2))) // Retry up to 3 times with 2 seconds delay
.onErrorReturn("Error fetching data");
}
}
বহু API কলের জন্য একই রেসপন্স আসতে পারে, যেমন ব্যবহারকারীর তথ্য। এ ধরনের ডেটা কিভাবে ক্যাশে করা যায় তা নিশ্চিত করা উচিত, যাতে সিস্টেমের কার্যক্ষমতা বাড়ানো যায়।
Best Practice:
API কল নিরাপদ করতে OAuth2 বা Basic Authentication ব্যবহার করা উচিত। যখন নিরাপত্তা গুরুত্বপূর্ণ, তখন API কলের জন্য Bearer token বা Basic Auth ব্যবহার করতে হবে।
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.http.HttpHeaders;
@Service
public class ApiService {
private final WebClient webClient;
public ApiService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com")
.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer YOUR_TOKEN")
.build();
}
public String fetchPostsWithAuth() {
return webClient.get()
.uri("/posts")
.retrieve()
.bodyToMono(String.class)
.block();
}
}
Spring Boot Client তৈরি করার সময় best practices অনুসরণ করলে কোড আরো মেনটেইনেবল, সিকিউর এবং স্কেলেবল হয়ে ওঠে। সঠিকভাবে WebClient বা RestTemplate ব্যবহার, Error Handling, Async Operations, Retry Logic, Timeout Configuration এবং Security এই সবই গুরুত্বপূর্ণ।
যদি আরো কোনো কনফিগারেশন বা কোড উদাহরণ দরকার হয়, জানাবেন! 😊
Read more